﻿using System.Collections.Generic;
using C556Backside.Commands.Organization;
using C556Backside.Models;
using C556Backside.ReturnModels;
using C556Backside.Services;
using C556Backside.ViewModel.Organization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Server.IIS.Core;
using Microsoft.EntityFrameworkCore;
using static C556Backside.ReturnModels.LoginReturn<C556Backside.Models.OrganizationModel>;

namespace C556Backside.Controllers;
[ApiController]
[Route("SelectIdentity/Organization")]
[EnableCors("CorsPolicy")]
public class OrganizationController
{
    public C556DBContext DBContext { get; set; }
    public OrganizationController(C556DBContext c556DBContext)
    {
        DBContext = c556DBContext;
    }
    [HttpPost]
    [Route("Login")]
    public ReturnModel Login([FromBody] COrganizationLogin login)
    {
        if (login == null)
        {
            return new ErrorReturn { ErrorMessage = "未提交数据" };
        }
        try
        {
            var result = DBContext.Organizations.SingleOrDefault(o => o.OrganizationName == login.UserName && o.OrganizationPassword == login.Password);
            if (result != null)
            {
                return new LoginReturn<OrganizationModel>(result) { Identity = LoginIdentity.Organization };
            }
            return new ErrorReturn { ErrorMessage = "未找到这位组织人员，请确认账号密码" };
        }
        catch
        {
            return new ErrorReturn { ErrorMessage = "出现异常，请联系系统管理人员" };
        }
    }
    [HttpPost]
    [Route("SignIn")]
    public ReturnModel SignIn([FromBody] COrganizationSignIn signIn)
    {
        if (signIn == null)
        {
            return new ErrorReturn { ErrorMessage = "未提交数据" };
        }
        try
        {
            var newOrganization = new OrganizationModel
            {
                OrganizationName = signIn.UserName,
                OrganizationPassword = signIn.Password,
                OrganizationTelephone = signIn.Telephone,
                Province = signIn.Province,
                City = signIn.City,
                Area = signIn.Area
            };
            var result = DBContext.Organizations.Add(newOrganization).Entity;
            DBContext.SaveChanges();
            return new SignInReturn<OrganizationModel>(result) { Identity = SignInReturn<OrganizationModel>.SignInIdentity.Organization };
        }
        catch
        {
            return new ErrorReturn { ErrorMessage = "出现异常，请联系管理人员" };
        }
    }
    [HttpGet]
    [Route("GetActivityByOrganizationID")]
    public List<VGetActivityByOrganizationIDModel> GetActivityByOrganizationID([FromQuery] CGetActivityByOrganizationIDModel model)
    {
        var list = new List<VGetActivityByOrganizationIDModel>();
        foreach (var item in DBContext.VoluntaryActivities.Include(e => e.ActivityTypeModel).Where(e => e.OrganizationID == model.OrganizationID && e.IsDelete == false).OrderByDescending(e => e.ActivityCreateTime))
        {
            var vModel = new VGetActivityByOrganizationIDModel
            {
                ActivityID = item.ActivityID,
                ActivityName = item.ActivityName,
                ActivityCreateTime = item.ActivityCreateTime.ToString(),
                ActivityCompleteTime = item.ActivityCompleteTime.ToString(),
                ActivityTypeName = item.ActivityTypeModel.ActivityTypeName,
                IsCompleted = item.ActivityComplete
            };
            list.Add(vModel);
        }
        return list;
    }
    [HttpPost]
    [Route("CreateActivity")]
    public VCreateActivity CreateActivity([FromBody] CCreateActivity model)
    {
        try
        {
            var type = DBContext.ActivityTypeModels.Single(o => o.ActivityTypeName == model.ActivityTypeName);
            var newActivity = new VoluntaryActivityModel()
            {
                ActivityName = model.ActivityName,
                OrganizationID = model.OrganizationID,
                ActivityTypeID = type.ActivityTypeID,
                ActivityCompleteTime = model.ActivityCompleteTime,
                Province = model.Province,
                VoluntaryContent = model.ActivityContent,
                City = model.City,
                Area = model.Area,
                DetailedAddress = model.DetailedAddress,
                ActivityComplete = false,
            };
            DBContext.VoluntaryActivities.Add(newActivity);
            DBContext.SaveChanges();
            return new VCreateActivity
            {
                ActivityID = newActivity.ActivityID,
                ActivityName = newActivity.ActivityName,
                VoluntaryContent = newActivity.VoluntaryContent
            };
        }
        catch
        {
            throw;
        }
    }
    [HttpGet]
    [Route("GetOrganitionCreateActivity")]
    public VGetOrganitionCreateActivity GetOrganitionCreateActivity([FromQuery] CGetOrganitionCreateActivity model)
    {
        try
        {
            var res = DBContext.VoluntaryActivities.Single(e => e.ActivityID == model.ActivityID);
            var vModel = new VGetOrganitionCreateActivity
            {
                ActivityCompleteTime = res.ActivityCompleteTime,
                ActivityContent = res.VoluntaryContent,
                ActivityName = res.ActivityName,
                ActivityTypeName = DBContext.ActivityTypeModels.Single(e => e.ActivityTypeID == res.ActivityTypeID).ActivityTypeName,
                Province = res.Province,
                City = res.City,
                Area = res.Area,
                DetailedAddress = res.DetailedAddress,
                IsComplete = res.ActivityComplete
            };
            return vModel;
        }
        catch
        {
            throw;
        }

    }
    [HttpPost]
    [Route("UpdateProperty")]
    public bool UpdateProperty([FromBody] CUpdateProperty model)
    {
        var res = DBContext.VoluntaryActivities.SingleOrDefault(e => e.ActivityID == model.ActivityID);
        res.ActivityName = model.ActivityName;
        res.VoluntaryContent = model.ActivityContent;
        res.ActivityCompleteTime = model.ActivityCompleteTime;
        res.Province = model.Province;
        res.City = model.City;
        res.Area = model.Area;
        res.DetailedAddress = model.DetailedAddress;
        var typeModel = DBContext.ActivityTypeModels.SingleOrDefault(e => e.ActivityTypeName == model.ActivityTypeName);
        res.ActivityTypeID = typeModel.ActivityTypeID;
        DBContext.SaveChanges();
        return true;
    }
    [HttpPost]
    [Route("DeleteActivity")]
    public bool DeleteActivity([FromBody] CDeleteActivity model)
    {
        var res = DBContext.VoluntaryActivities.Single(e => e.ActivityID == model.ActivityID);
        res.IsDelete = true;
        return DBContext.SaveChanges() > 0;
    }
    [HttpGet]
    [Route("ConfirmActivityComplete")]
    public bool ConfirmActivityComplete([FromQuery] CConfirmActivityComplete model)
    {
        var res = DBContext.VoluntaryActivities.Single(o => o.ActivityID == model.ActivityID);
        res.ActivityComplete = true;
        return DBContext.SaveChanges() > 0;
    }

    [HttpGet]
    [Route("GetMyActivityVoluntary")]
    public List<VGetMyActivityVoluntary> GetMyActivityVoluntary([FromQuery] CGetMyActivityVoluntary model)
    {
        var va = DBContext.VAVRelationshipsTable.Where(o => o.ActivityID == model.Id);
        var vID = va.Select(o => o.VoluntaryID).ToArray();
        var v = DBContext.Voluntary.Where(o => vID.Contains(o.VoluntaryID));
        return v.Select(o => new VGetMyActivityVoluntary { VoluntaryName = o.VoluntaryName, VoluntaryTelephone = o.VoluntaryTelephone, Province = o.Province, City = o.City, Area = o.Area }).ToList();
    }
}

